package gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import model.Stack;
import model.TaskInfo;
import service.Service;
import dao.Dao;

public class AllStacksPanel extends TabledFilteringPanel {

    public AllStacksPanel() {
	super();

	String[] columnNamesArray = { "Name", "Number of trays",
		"Current process", "Next process", "Start time", "Finish time" };

	for (int i = 0; i < columnNamesArray.length; i++) {
	    columnNames.add(columnNamesArray[i]);
	}

	generateDataVector();

	setData(columnNames, data);

    }

    @Override
    protected void generateDataVector() {
	ArrayList<Stack> allStacks = new ArrayList<Stack>();
	allStacks.addAll(Dao.getInstance().getStacks());
	allStacks.addAll(Dao.getInstance().getFinished());
	allStacks.addAll(Dao.getInstance().getWastes());
	Collections.sort(allStacks, new StackAlternativeCompoarator());
	for (Stack x : allStacks) {
	    TaskInfo next = x.getNextProcess();
	    Vector<Object> dataRow = new Vector<Object>();
	    dataRow.add(x);
	    dataRow.add(x.getNumberOfTrays());
	    dataRow.add(x.getCurrentProcess().getProcess().getName());
	    dataRow.add(next == null ? "" : next.getProcess().getName());
	    dataRow.add(Service.getInstance().displayTime(
		    x.getTasks().get(0).getStartTime()));
	    dataRow.add(Service.getInstance().displayTime(
		    x.getTasks().get(x.getTasks().size() - 1).getFinishTime()));
	    data.add(dataRow);
	}
    }

    private class StackAlternativeCompoarator implements Comparator<Stack> {

	@Override
	public int compare(Stack arg0, Stack arg1) {
	    return (arg0.getTasks().get(0).getStartTime().compareTo(arg1
		    .getTasks().get(0).getStartTime()));
	}

    }
}
